package edu.nus.mtech.sem1.ca.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import edu.nus.mtech.sem1.ca.exception.DataInputFormatException;

/**
 * Arsenal of utility methods that doesnt fall under any category
 *
 */
public class TelephonyUtils {

	private static SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
	private static SimpleDateFormat formatterDateTime = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
	
	public static String getStringFromDate(Date date){
		String strDate = "";
		try{
			if (date == null) strDate = "";
			else
			strDate = formatter.format(date);
		}catch(Exception e){
			e.printStackTrace();
		}
		return strDate;
	}
	
	public static String getDateTimeStringFromDate(Date date){
		String strDate = "";
		try{
			if (date == null) strDate = "";
			else
			strDate = formatterDateTime.format(date);
		}catch(Exception e){
			e.printStackTrace();
		}
		return strDate;
	}
	
	public static Float getSafeFloatFromString(String string) {
		float returnFloat=0.0F;
		if (string==null){
			return returnFloat;
		}
		else{
			try {
				returnFloat=Float.parseFloat(string);
			} catch (NumberFormatException e) {
				//There would be spaces as values
				returnFloat=0.0F;
				e.printStackTrace();
			}
		}
		return returnFloat;
	}
	
	public static Integer getSafeIntegerFromString(String string) {
		int returnInt=0;
		if (string==null){
			return returnInt;
		}
		else{
			try {
				returnInt=Integer.parseInt(string);
			} catch (NumberFormatException e) {
				//There would be spaces as values
				returnInt=0;
				e.printStackTrace();
			}
		}
		return returnInt;
	}
	
	public static Date getDateFromString(String dateString) throws DataInputFormatException{
		Date date=null;
		try {
			date=formatter.parse(dateString);
		} catch (ParseException e) {
			//Oh the date is invalid. throw Exception. You could also default to Calendar.getInstance 
			//but that would be too misleading
			e.printStackTrace();
			throw new DataInputFormatException("Date field incorrect. Should be DD/MM/YYYY");
		}
		return date;
		
	}
	
	/**
	 * Returns a hashcode irrespective of whether an object is null or not
	 * @param object
	 * @return
	 */
	public static int getHashCode(Object object){
		if (object==null){
			return 0;
		}
		else{
			return object.hashCode();
		}
	}
	
	/**
	 * The CSV has NA as the Datefield. Returns "now" as the default date
	 * @param naDate
	 * @return
	 * @throws DataInputFormatException
	 */
	public static Date getSafeDate(String naDate) throws DataInputFormatException{ 
		//All other values except NA should throw a DataInputFormatException
		Date returnDate=null;
		
		if (naDate.equalsIgnoreCase("NA") || "".equals(naDate) || naDate==null){ //cant be null coz this is a CSV
			returnDate=Calendar.getInstance().getTime(); //now
		}
		else{
			returnDate=TelephonyUtils.getDateFromString(naDate);
		}
		
		return returnDate;
	}


	public static String getSafeString(String notSoSafeString){
		
		//Should ideally use StringBuilder and nullify the input String, but since this method involves only a single operation and I am so lazy....
		if (notSoSafeString!=null){
			notSoSafeString=notSoSafeString.trim();
		}
		else{
			notSoSafeString="";
		}
		
		//ah yeah. it is to be named as nowSafeString
		return notSoSafeString;
		
	}
}
